package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TraveServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper mapper;
    @Autowired
    private TravelContentMapper contentMapper;

    @Autowired
    private TravelCommendMapper commendMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PointMsgMapper pointMsgMapper;


    @Override
    public PageInfo<Travel> selectForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = mapper.selectList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        Date date = new Date();
        travel.setLastUpdateTime(date);
        TravelContent content = travel.getContent();
        if (travel.getId() == null) {
            travel.setCreateTime(date);
            mapper.insert(travel);
            content.setId(travel.getId());
            contentMapper.insert(content);
        } else {
            mapper.updateByPrimaryKey(travel);
            content.setId(travel.getId());
            contentMapper.updateByPrimaryKey(content);
        }
    }

    @Override
    public Travel selectById(Long id, Long purpose) {
        Travel travel = mapper.selectByPrimaryKey(id);
        //purpose = 1,则本次查询为浏览查询,记录浏览次数+1
        if ((purpose == 1)) {
            travel.setViewCount(travel.getViewCount()+1);
            mapper.updateViewCount(travel);
        }
        return travel;
    }

    @Override
    public TravelContent getContentById(Long id) {

        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateState(Travel travel) {
        //发布游记
        if (travel.getState() == Travel.STATE_RELEASE) {
            travel.setReleaseTime(new Date());
            User author = mapper.getAuthorByTravelId(travel.getId());
            //每发布一篇游记给10积分
            userMapper.addPoint(author.getId(),10);
            //插入积分增加记录
            PointMsg pointMsg = new PointMsg();
            pointMsg.setCreateTime(new Date());
            pointMsg.setUser(author);
            String msg = "您的游记"+"<<"+travel.getTitle()+">>"+"发布成功,获得10积分";
            pointMsg.setMsg(msg);
            pointMsgMapper.insert(pointMsg);
        }
        mapper.updateState(travel);
        //如果是下架操作,则同时删除该游记的推荐
        if (travel.getState() == Travel.STATE_REFUSE) {
            commendMapper.deleteByPrimaryKey(travel.getId());

        }
    }

    @Override
    public PageInfo<Travel> regionHotTravelList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = mapper.selsectRegionHotTravel(qo);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Travel> getDaily(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = mapper.selectDaily(qo);
        return new PageInfo<>(list);
    }
}
